//
//  BIDBaseCollectionViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/16.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDBaseCollectionViewController.h"

@interface BIDBaseCollectionViewController ()<BIDBaseCollectionViewDelegate>

@end

@implementation BIDBaseCollectionViewController
@synthesize myCollectionView;
@synthesize countPerPage;
@synthesize curPage;
@synthesize dataSourceArr;
@synthesize isRegister;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    countPerPage = 15;
    curPage = 0;
    dataSourceArr = [NSMutableArray array];
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setItemSize:CGSizeMake(10, 10)];
    [flowLayout setMinimumLineSpacing:0];
    [flowLayout setMinimumInteritemSpacing:0];
    myCollectionView = [[BIDBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height-64) collectionViewLayout:flowLayout];
    myCollectionView.myDelegate = self;
    [self.view addSubview:myCollectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    myCollectionView.myDelegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    UINavigationController *navController = self.navigationController;
    if(navController)
    {
        if([navController.viewControllers containsObject:self])
        {
        }
        else
        {
            myCollectionView.myDelegate = nil;
        }
    }
}

- (void)loadData
{
}

/**
 *  结束下拉刷新和上拉加载更多的旋转动画
 */
- (void)externalTask
{
    [self.myCollectionView endRefresh];
    [self.myCollectionView hasNoMoreData];
}

/**
 自动判断是否需要显示空视图
 */
- (void)autoDecideToShowEmptyView
{
    if(self.dataSourceArr.count == 0)
    {
        [self.myCollectionView showEmptyView];
    }
    else
    {
        [self.myCollectionView hideEmptyView];
    }
}

#pragma mark - BIDBaseListViewDelegate
/**
 *  下拉刷新
 */
- (void)refresh
{
    NSLog(@"refresh");
    curPage = 0;
    NSArray *arr = dataSourceArr;
    if(arr.count>0)
    {
        if([arr[0] isKindOfClass:[NSArray class]] && arr.count>1)
        {
            //多个section
            NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, arr.count)];
            [myCollectionView deleteSections:indexSet];
            [dataSourceArr removeAllObjects];
        }
        else
        {
            //只有一个section
            NSMutableArray *indexPathArr = [NSMutableArray array];
            for(int i=0; i<arr.count; i++)
            {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [indexPathArr addObject:indexPath];
            }
            [dataSourceArr removeAllObjects];
            [myCollectionView deleteItemsAtIndexPaths:indexPathArr];
        }
    }
    [self loadData];
}
/**
 *  上拉加载更多
 */
- (void)loadMoreData
{
    [self loadData];
}

#pragma mark - BIDBaseCollectionViewController
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

@end
